#include "prescriptions.h"
#include "messagetype.h"
#include <QMessageBox>
#include <QDebug>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QPalette>
#include <QColor>
#include <QTextEdit>

Prescriptions::Prescriptions(QTcpSocket *socket, QWidget *parent)
    : QWidget(parent), tcpSocket(socket) {
    // Set window title and fixed size
    setWindowTitle("查看处方与缴费");
    setFixedSize(600, 700); // 增大窗口大小

    // Set background color
    QPalette palette = this->palette();
    palette.setColor(QPalette::Window, QColor(248, 248, 255));
    this->setPalette(palette);
    this->setAutoFillBackground(true);

    // Main layout
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(20, 20, 20, 20);
    mainLayout->setSpacing(15);

    // Title label
    QLabel *label = new QLabel("<h2>查看处方与缴费</h2>", this);
    label->setAlignment(Qt::AlignCenter);
    mainLayout->addWidget(label);

    // Form layout for patient information
    QFormLayout *formLayout = new QFormLayout();
    formLayout->setSpacing(15);

    // Create and style input fields
    nameEdit = new QLineEdit(this);
    nameEdit->setPlaceholderText("请输入患者姓名");
    setInputFieldStyle(nameEdit);

    idEdit = new QLineEdit(this);
    idEdit->setPlaceholderText("请输入患者ID");
    setInputFieldStyle(idEdit);

    // Add fields to form layout
    formLayout->addRow("患者姓名", nameEdit);
    formLayout->addRow("患者ID", idEdit);

    mainLayout->addLayout(formLayout);

    // Button layout for query and back
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    QPushButton *queryButton = createStyledButton("查询", "#4CAF50");
    QPushButton *backButton = createStyledButton("返回", "#f44336");
    buttonLayout->addWidget(queryButton);
    buttonLayout->addWidget(backButton);

    mainLayout->addLayout(buttonLayout);

    // Prescription display
    prescriptionDisplay = new QTextEdit(this);
    prescriptionDisplay->setReadOnly(true);
    prescriptionDisplay->setStyleSheet("padding: 10px; border: 1px solid #ccc; border-radius: 5px; background-color: #f9f9f9;");
    mainLayout->addWidget(prescriptionDisplay);

    // Set main layout
    setLayout(mainLayout);

    // Connect buttons to slots
    connect(queryButton, &QPushButton::clicked, this, &Prescriptions::onQueryButtonClicked);
    connect(backButton, &QPushButton::clicked, this, &Prescriptions::onBackButtonClicked);
}

// Function to style input fields
void Prescriptions::setInputFieldStyle(QLineEdit *lineEdit) {
    lineEdit->setStyleSheet("padding: 8px; border: 1px solid #ccc; border-radius: 5px;");
    lineEdit->setFixedHeight(35);
}

// Function to create styled buttons
QPushButton* Prescriptions::createStyledButton(const QString &text, const QString &color) {
    QPushButton *button = new QPushButton(text, this);
    button->setStyleSheet(QString("background-color: %1; color: white; border: none; border-radius: 5px; padding: 10px;").arg(color));
    button->setFixedHeight(40);
    return button;
}

void Prescriptions::onQueryButtonClicked() {
    // 获取输入的患者姓名和ID
    QString patientName = nameEdit->text();
    QString patientId = idEdit->text();

    // 构造查询请求的数据包
    QMap<QString, QString> queryData;
    queryData.insert("type", PRESCRIPTIONDATABASE);
    queryData.insert("databaseoperator", QString::number(SELECT));
    queryData.insert("patientname", patientName);
    queryData.insert("patientid", patientId);

    QByteArray data = mapToByteArrayCustom(queryData);

    // 连接服务器并发送请求
    if (tcpSocket->state() == QAbstractSocket::UnconnectedState) {
        tcpSocket->connectToHost("127.0.0.1 ", 12345);
    }
    if (tcpSocket->waitForConnected(5000)) {
        tcpSocket->write(data);
        tcpSocket->flush();

        // 等待服务器的响应
        if (tcpSocket->waitForReadyRead(5000)) {
            QByteArray responseData = tcpSocket->readAll();

            // 解析响应的数据
            QList<QMap<QString, QString>> prescriptionRecords = convertByteArrayToQMapArrayCustom(responseData);

            // 展示处方信息
            if (!prescriptionRecords.isEmpty()) {
                QString displayText;
                for (const QMap<QString, QString> &record : prescriptionRecords) {
                    displayText += "处方: " + record.value("prescr") + "\n";
                    displayText += "医生: " + record.value("doctorname") + "\n";
                    displayText += "-----------------------\n";
                }
                prescriptionDisplay->setText(displayText);
            } else {
                prescriptionDisplay->setText("未找到相关处方记录");
            }
        } else {
            // 如果未能接收到响应，显示错误提示
            prescriptionDisplay->setText("未能接收到服务器响应，请稍后重试");
        }
    } else {
        // 如果连接服务器失败，显示错误提示
        prescriptionDisplay->setText("无法连接到服务器，请检查网络连接");
    }
}

void Prescriptions::onBackButtonClicked() {
    this->close();
}
